package com.ruoyi.equ.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.equ.domain.EquBasic;
import com.ruoyi.equ.mapper.EquBasicMapper;
import com.ruoyi.equ.service.IEquBasicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 设备基本台账Service业务层处理
 * 
 * @author lgq
 * @date 2023-10-07
 */
@Service
public class EquBasicServiceImpl implements IEquBasicService
{
    private static final Logger log = LoggerFactory.getLogger(EquBasicServiceImpl.class);

    @Autowired(required = false)
    private EquBasicMapper equBasicMapper;

    /**
     * 查询设备基本台账
     * 
     * @param id 设备基本台账主键
     * @return 设备基本台账
     */
    @Override
    public EquBasic selectEquBasicById(String id)
    {
        return equBasicMapper.selectEquBasicById(id);
    }

    /**
     * 查询设备状态
     */
    @Override
    public List<Map<String, Object>> selectEquBasicCount() {
        return equBasicMapper.selectEquBasicCount();
    }

    /**
     * 查询多个设备基本信息
     * @param ids
     * @return
     */
    @Override
    public List<EquBasic> selectEquBasicByIds(String[] ids) {
        return equBasicMapper.selectEquBasicByIds(ids);
    }

    /**
     * 根据标签查询设备
     * @param label
     * @return
     */
    @Override
    public List<EquBasic> selectEquBasicByLabels(String label) {
        List<EquBasic> equBasicList = new ArrayList<>();
        if(StringUtils.isNotEmpty(label)) {
            //分割labe标签并分别查询
            String[] labels = label.split(",");
            for(int i = 0; i < labels.length; i++) {
                EquBasic equBasic = new EquBasic();
                equBasic.setLabels(labels[i]);
                equBasic.setState("1");
                equBasicList.addAll(equBasicMapper.selectEquBasicList(equBasic));
            }
        }
        return equBasicList.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(EquBasic::getEquNum))), ArrayList::new));
    }

    /**
     * 根据设备标签查询后再根据分组查询同组设备
     *
     * @param label
     * @return
     */
    @Override
    public Map<String, List<EquBasic>> selectEquBasicByGroupAndLabel(String label) {
        List<EquBasic> equBasicList = equBasicMapper.selectEquBasicByLabels(label);
        Map<String, List<EquBasic>> map = new HashMap<>();
        //根据equBasicList中的groups分组
        map = equBasicList.stream().sorted(Comparator.comparing(EquBasic::getEquNum))
                .collect(Collectors.groupingBy(EquBasic::getEquGroups));
        TreeMap<String, List<EquBasic>> sortedDataMap = new TreeMap<>(map);
        return sortedDataMap;
    }

    @Override
    public EquBasic selectEquBasicByNum(String equNum) {
        return null;
    }


    /**
     * 根据设备标识获取设备基本信息
     * @param equNum
     * @return
     */
    @Override
    public EquBasic selectEquBasicByEquNum(String equNum, String state) {
        return equBasicMapper.selectEquBasicByEquNum(equNum,state);
    }


    /**
     * 设备信息导入
     * @param equBasicList
     * @return
     */
    @Override
    public String importEqu(List<EquBasic> equBasicList, boolean updateSupport) {
        if (StringUtils.isNull(equBasicList) || equBasicList.size() == 0)
        {
            throw new ServiceException("导入设备数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (EquBasic equ : equBasicList)
        {
            try
            {
                // 验证是否存在这个设备
                EquBasic e = equBasicMapper.selectEquBasicByNum(equ.getEquNum());
                if (StringUtils.isNull(e))
                {
                    equ.setId(IdUtils.fastSimpleUUID());
                    this.insertEquBasic(equ);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备唯一编号 " + equ.getEquNum() + " 导入成功");
                }
                else if (updateSupport)
                {
                    this.updateEquBasicByEquNum(equ);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备唯一编号 " + equ.getEquNum() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备唯一编号 " + equ.getEquNum() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备唯一编号 " + equ.getEquNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询设备基本台账列表
     * 
     * @param equBasic 设备基本台账
     * @return 设备基本台账
     */
    @Override
    public List<EquBasic> selectEquBasicList(EquBasic equBasic)
    {
        return equBasicMapper.selectEquBasicList(equBasic);
    }


    /**
     * 查询设备信息
     *
     * @param equNums 设备编码
     * @return 设备基本信息
     */
    @Override
    public List<EquBasic> selectEquBasicByNums(String[] equNums) {
        return equBasicMapper.selectEquBasicByNums(equNums);
    }

    /**
     * 新增设备基本台账
     * 
     * @param equBasic 设备基本台账
     * @return 结果
     */
    @Override
    public int insertEquBasic(EquBasic equBasic)
    {
        return equBasicMapper.insertEquBasic(equBasic);
    }

    /**
     * 修改设备基本台账
     * 
     * @param equBasic 设备基本台账
     * @return 结果
     */
    @Override
    public int updateEquBasic(EquBasic equBasic)
    {
        return equBasicMapper.updateEquBasic(equBasic);
    }


    /**
     * 修改设备状态
     *
     * @param equBasic 设备基本信息
     * @return 结果
     */
    @Override
    public int updateEquBasicByNum(EquBasic equBasic) {
        return equBasicMapper.updateEquBasicByNum(equBasic);
    }


    /**
     * 修改设备证书信息
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateEquInvalidity() {
        return equBasicMapper.updateEquInvalidity();
    }

    /**
     * 修改设备状态
     *
     * @param ids 设备编码
     * @return 结果
     */
    @Override
    public int updateEquBasics(String state, String[] ids) {
        return equBasicMapper.updateEquBasics(state, ids);
    }

    /**
     * 根据设备名称更新设备信息
     * @param equBasic
     * @return
     */
    @Override
    public int updateEquBasicByEquNum(EquBasic equBasic) {
        return equBasicMapper.updateEquBasicByEquNum(equBasic);
    }

    /**
     * 批量删除设备基本台账
     * 
     * @param ids 需要删除的设备基本台账主键
     * @return 结果
     */
    @Override
    public int deleteEquBasicByIds(String[] ids)
    {
        return equBasicMapper.deleteEquBasicByIds(ids);
    }

    /**
     * 删除设备基本台账信息
     * 
     * @param id 设备基本台账主键
     * @return 结果
     */
    @Override
    public int deleteEquBasicById(String id)
    {
        return equBasicMapper.deleteEquBasicById(id);
    }
}
